package cn.school.controller.student;


import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.entity.TExamSubject;
import cn.school.student.service.TExamSubjectService;
import cn.school.sys.entity.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (TExamSubject)表控制层
 *
 * @author makejava
 * @since 2023-02-16 15:12:53
 */
@RestController
@RequestMapping("tExamSubject")
public class TExamSubjectController {
    /**
     * 服务对象
     */
    @Autowired
    private TExamSubjectService tExamSubjectService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 分页查询
     *
     * @param tExamSubject 筛选条件
     * @param page,size      分页对象
     * @return 查询结果
     */
    @GetMapping("/query")
    @ApiOperation("query=>分页查询题目信息")
    public CommonResult<CommonPage<TExamSubject>> queryByPage(TExamSubject tExamSubject, Integer pageNum , Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(tExamSubjectService.queryByPage(tExamSubject,PageRequest.of((pageNum-1),pageSize))));
    }
    @PostMapping("/tExamSubjectExcel")
    @ApiOperation("tExamSubjectExcel=>导入题库")
    public CommonResult<Integer>  tExamSubjectExcel(@RequestParam("file")  MultipartFile multipartFile)throws IOException, ApiException {
        return this.tExamSubjectService.tExamSubjectExcel(multipartFile);
    }
    @PostMapping("/tExamSubjectWord")
    public CommonResult<List<TExamSubject>> tExamSubjectWord(@RequestParam("file") MultipartFile file,@RequestParam("grade")String grade,
                                                  @RequestParam("major")String major,@RequestParam("subjectId")String subjectId) throws Exception {
        List<TExamSubject> list = this.tExamSubjectService.tExamSubjectWord(file,grade,major,subjectId);
        return CommonResult.success(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("queryById=>查询详细题目信息")
    public CommonResult<TExamSubject> queryById(@PathVariable("id") Integer id) {
        return CommonResult.success(this.tExamSubjectService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tExamSubject 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation("insert=>新增题目信息")
    public CommonResult<TExamSubject> add(@RequestBody TExamSubject tExamSubject) {
        SysUser sysUser = SysUserUtils.getSysUser();
        tExamSubject.setCreateBy(sysUser.getUsername());
        tExamSubject.setCreateTime(new Date());
        Integer insert = this.tExamSubjectService.insert(tExamSubject);
        if (insert!=0){
            return CommonResult.success();
        }
        return CommonResult.failed();

    }


    /**
     * 新增数据
     *
     * @param tExamSubject 实体
     * @return 添加试卷
     */
    @PostMapping("/insertTExamSubject")
    @ApiOperation("insert=>新增题目信息")
    public CommonResult<TExamSubject> insertTExamSubject(@RequestBody TExamSubject tExamSubject) {
        SysUser sysUser = SysUserUtils.getSysUser();
        tExamSubject.setCreateBy(sysUser.getUsername());
        tExamSubject.setCreateTime(new Date());

        Integer insert = this.tExamSubjectService.insert(tExamSubject);
        if (insert!=0){
            return CommonResult.success();
        }
        return CommonResult.failed();

    }

    /**
     * 编辑数据
     *
     * @param tExamSubject 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @ApiOperation("update=>更新题目信息")
    public CommonResult<TExamSubject> edit(@RequestBody TExamSubject tExamSubject) {
        SysUser sysUser = SysUserUtils.getSysUser();
        tExamSubject.setModifyBy(sysUser.getUsername());
        tExamSubject.setModifyTime(new Date());

        Integer insert = this.tExamSubjectService.update(tExamSubject);


        if (insert!=0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation("delete=>删除题目信息")
    public CommonResult<Boolean> deleteById(Integer id) {
        boolean b = this.tExamSubjectService.deleteById(id);
        if (b){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

}

